package com.music.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.music.server.pojo.*;
import com.music.server.mapper.SongMapper;
import com.music.server.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.music.server.vo.SongVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 歌曲 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-08
 */
@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {

    @Autowired
    private CollectService collectService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ListSongService listSongService;

    @Transactional
    @Override
    public boolean deleteById(Integer id) {
        //删歌曲
        this.removeById(id);
        //删歌曲收藏
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq("song_id",id);
        collectService.remove(collectQueryWrapper);
        //删除歌曲评论
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("song_id",id);
        commentService.remove(commentQueryWrapper);
        //删歌单与歌曲的对应关系
        QueryWrapper<ListSong> listSongQueryWrapper = new QueryWrapper<>();
        listSongQueryWrapper.eq("song_id",id);
        listSongService.remove(listSongQueryWrapper);
        return true;
    }

    @Override
    public Map<String, Object> page(Integer pageNum, Integer size, SongVo songVo) {
        List<Integer> songIds = new ArrayList<>();
        if (songVo.getSongListId()!=null){
            QueryWrapper<ListSong> listSongQueryWrapper = new QueryWrapper<>();
            listSongQueryWrapper.eq("song_list_id",songVo.getSongListId());
            List<ListSong> list = listSongService.list(listSongQueryWrapper);
            for (ListSong listSong : list) {
                songIds.add(listSong.getSongId());
            }

        }
        Page<Song> songPage = new Page<>(pageNum,size);
        QueryWrapper<Song> songQueryWrapper = new QueryWrapper<>();
        if (!CollectionUtils.isEmpty(songIds)){
            songQueryWrapper.in("id",songIds);
        }else{
            if (songVo.getSongListId()!=null)  return null;

        }
        if (songVo.getSingerId()!=null){
            songQueryWrapper.eq("singer_id",songVo.getSingerId());
        }
        if (songVo.getName()!=null){
            songQueryWrapper.like("name",songVo.getName());
        }

        this.page(songPage,songQueryWrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total",songPage.getTotal());
        List<Song> songList = songPage.getRecords();
        data.put("list",songList);
        return data;
    }
}
